package com.bawei.module_mine.ui

import android.graphics.Bitmap
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.View
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.alibaba.android.arouter.facade.annotation.Route
import com.bawei.lib_mvi.base.BaseActivity
import com.bawei.module_mine.R
import com.bawei.module_mine.databinding.ActivityDimensionalBinding
import com.journeyapps.barcodescanner.BarcodeEncoder

@Route(path = "/mine/DimensionalActivity")
class DimensionalActivity : BaseActivity() {
    private val binding by lazy {
        ActivityDimensionalBinding.inflate(layoutInflater)
    }

    private val handler = Handler(Looper.getMainLooper())

    override fun initView() {
        super.initView()
        // ... 你的其他初始化代码

        // 生成二维码内容（比如用当前时间戳或用户ID等）
        val qrContent = System.currentTimeMillis().toString()
        val qrBitmap = generateQRCode(qrContent)
        binding.ivQrCode.setImageBitmap(qrBitmap)
        binding.icBack.setOnClickListener {
            finish()
        }
    }

    private fun generateQRCode(content: String): Bitmap? {
        return try {
            val barcodeEncoder = BarcodeEncoder()
            barcodeEncoder.encodeBitmap(content, com.google.zxing.BarcodeFormat.QR_CODE, 400, 400)
        } catch (e: Exception) {
            null
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        handler.removeCallbacksAndMessages(null) // 停止所有回调
    }

    override fun getLayoutRootView(): View {
        return binding.root
    }
}